< previous page page_447 next page >

Page 447
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdDisplay_Click()
   Dim oEmp As Employee

   ' Retrieve Search Criteria from Collection
   ' To generate the error set the txtSearch = '99'
   Set oEmp = mcolEmps.Item(txtSearch)

   ' Display the Employee Name
   oEmp.EmployeeName
End Sub
If you enter 99 in the txtSearch text box, youll receive a Type Mismatch error from Visual Basic when you attempt to perform the Item() method on the Collection object. This is because its trying to put a Form object in a pointer thats declared to hold an Employee object.
9f31265bb40b592b1929e18a47ab28e5.gif
TIP
Be careful using the
Collection object in your code because you can store any type of object within it. This could cause problems at runtime.
Create Your Own Collection Class
You can easily solve the problem of using a generic Collection class by just not using it. You can create your own collection class by wrapping the Collection class into your own class module. Youll need to create an Employees class (notice the letter s on the end of Employees) that duplicates the methods of the Collection class, but in this case, the Add method only accepts employee information, not any type of object.
Examples Filename: Emp3.frm
In the sample project shown in Figure 22.4, youll find an Employee class and an Employees class.
8160-0447a.gif
Figure 22.4
Creating your own 
Collection class.

 
< previous page page_447 next page >

If you like this book, buy it!